package cn.schoolwow.util.test.module.compare.jsonarray.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.beanCopy.BeanCopy;
import cn.schoolwow.util.domain.compare.jsonarray.UpdateJSONArrayCompare;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class InitialCompareJSONArrayDataFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setBeforeJSONArray(flowContext);
        setAfterJSONArray(flowContext);
        setAddCompareList(flowContext);
        setUpdateJSONArrayCompare(flowContext);
        setDeleteCompareList(flowContext);
    }

    @Override
    public String name() {
        return "初始化比较JSON列表参数";
    }

    private void setBeforeJSONArray(FlowContext flowContext){
        JSONArray beforeJSONArray = new JSONArray();
        for(int i=0;i<4;i++){
            JSONObject addProjectTimeNode = new JSONObject();
            addProjectTimeNode.put("name", "时间节点"+i);
            addProjectTimeNode.put("deadline", LocalDate.now().plusMonths(i));
            beforeJSONArray.add(addProjectTimeNode);
        }
        flowContext.putTemporaryData("beforeJSONArray", beforeJSONArray);
    }

    private void setAfterJSONArray(FlowContext flowContext) throws IOException, ClassNotFoundException {
        JSONArray beforeJSONArray = (JSONArray) flowContext.checkData("beforeJSONArray");

        JSONArray afterJSONArray = new JSONArray();
        JSONArray cloneArray = BeanCopy.cloneJSONArray(beforeJSONArray);
        afterJSONArray.addAll(cloneArray);

        flowContext.putTemporaryData("afterJSONArray", afterJSONArray);
    }

    private void setAddCompareList(FlowContext flowContext){
        JSONArray afterJSONArray = (JSONArray) flowContext.checkData("afterJSONArray");

        JSONArray addCompareArray = new JSONArray();

        JSONObject addProjectTimeNode = new JSONObject();
        addProjectTimeNode.put("name", "时间节点5");
        addProjectTimeNode.put("deadline", LocalDate.now().plusMonths(12));
        afterJSONArray.add(addProjectTimeNode);

        addCompareArray.add(addProjectTimeNode);

        flowContext.putTemporaryData("addCompareArray", addCompareArray);
    }

    private void setUpdateJSONArrayCompare(FlowContext flowContext){
        List<UpdateJSONArrayCompare> updateJSONArrayCompareArray = new ArrayList<>();

        JSONArray beforeJSONArray = (JSONArray) flowContext.checkData("beforeJSONArray");
        JSONArray afterJSONArray = (JSONArray) flowContext.checkData("afterJSONArray");

        for(int i=0;i<2;i++){
            JSONObject addProjectTimeNode = afterJSONArray.getJSONObject(i);
            addProjectTimeNode.put("deadline", LocalDate.now().plusMonths(10+i));

            UpdateJSONArrayCompare updateJSONArrayCompare = new UpdateJSONArrayCompare();
            updateJSONArrayCompare.beforeObject = beforeJSONArray.getJSONObject(i);
            updateJSONArrayCompare.afterObject = afterJSONArray.getJSONObject(i);
            updateJSONArrayCompareArray.add(updateJSONArrayCompare);
        }
        flowContext.putTemporaryData("updateJSONArrayCompareArray", updateJSONArrayCompareArray);
    }

    private void setDeleteCompareList(FlowContext flowContext){
        JSONArray afterJSONArray = (JSONArray) flowContext.checkData("afterJSONArray");

        JSONArray deleteCompareArray = new JSONArray();
        JSONObject removeAddProjectTimeNode = (JSONObject) afterJSONArray.remove(2);
        deleteCompareArray.add(removeAddProjectTimeNode);

        flowContext.putTemporaryData("deleteCompareArray", deleteCompareArray);
    }

}
